#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/configfs.h>
#include <linux/kernel.h>
#include <linux/kobject.h>


// 定义kobject指针
struct kobject* mykobject1;
struct kobject* mykobject2;

//定义kset指针
struct kset* mykset;

//定义kobj_type结构体
struct kobj_type mytype;

static int mykset_init(void)
{
    int ret = 0;

    //创建并添加kset
    mykset = kset_create_and_add("mykset", NULL, NULL);

    //开空间并初始化mykobject
    mykobject1 = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    mykobject1->kset = mykset;
    ret = kobject_init_and_add(mykobject1, &mytype, NULL, "%s", "mykobject1");
    if (ret > 0)
    {
        printk("init kobject1 failed");
        return -1;
    }
    mykobject2 = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    mykobject2->kset = mykset;
    ret = kobject_init_and_add(mykobject2,  &mytype, NULL, "%s", "mykobject2");
    if (ret > 0)
    {
        printk("init kobject2 failed");
        return -1;
    }

    return 0;

}

//模块卸载函数

static void mykset_exit(void)
{
    //释放kobject引用计数
    kobject_put(mykobject1);
    kobject_put(mykobject2);

}
module_init(mykset_init);
module_exit(mykset_exit);

MODULE_LICENSE("GPL");
